<!--
 * @author  dumeng
 * @desc 活动报名-查看评论列表
-->
<template>
  <div class="hdbm-comment">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.commentContent"
          placeholder="输入评论内容搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button
            class="filter-item"
            type="primary"
            @click="handleFilter"
          >搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </section>
      </section>
    </el-collapse-transition>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>评论</span>
      </section>
      <section class="main-buttons-btns" />
      <el-button
        style="margin-left: 10px"
        @click="
          () => {
            $router.back();
          }
        "
      >返回</el-button>
    </section>
    <section class="main-table">
      <el-table :data="list" :list-loading="listLoading" style="width: 100%">
        <el-table-column type="index" width="50" label="序号" />
        <el-table-column prop="commentContent" label="评论内容">
          <template slot-scope="scope">
            <el-row>
              <el-col
                :span="24"
              >评论内容：{{ scope.row.commentContent }}</el-col>
              <el-col :span="24">评论人：{{ scope.row.userRealName }}</el-col>
              <el-col :span="12">评论状态：{{ scope.row.statusName }}</el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="userImgs" label="评论图片">
          <template slot-scope="scope">
            <el-image
              v-for="item in scope.row.userImgs? scope.row.userImgs.split(','): []"
              :key="item"
              class="comment-img"
              :z-index="99999"
              :src="view + item"
              :preview-src-list="[view + item]"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="crtTime" label="评论时间" />
        <el-table-column label="操作" fixed="right" width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleView(scope.row)"
            >溯源</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleUpdate(scope.row)"
            >修改状态</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
    <el-dialog
      v-el-drag-dialog
      :close-on-click-modal="false"
      :visible.sync="dialog4comment"
      title="评论设置"
      center
    >
      <el-form ref="form" label-width="150px">
        <el-form-item label="评论状态">
          <el-radio-group v-model="commentStatus">
            <el-radio
              v-for="(item, index) in dict.PLZT"
              :key="index"
              :label="item.dictValue"
            >{{ item.dictName }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          style="padding: 8px 30px"
          @click="dialog4comment = false"
        >关 闭</el-button>
        <el-button
          type="primary"
          style="padding: 8px 30px"
          @click="handleComit()"
        >确 认</el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-el-drag-dialog
      :close-on-click-modal="false"
      :visible.sync="dialog4commentParent"
      title="评论设置"
      center
      width="80%"
    >
      <div class="main-table">
        <el-table
          :data="list4parent"
          :list-loading="listLoading4parent"
          style="width: 100%"
        >
          <el-table-column type="index" width="50" />
          <el-table-column prop="commentContent" label="评论内容">
            <template slot-scope="scope">
              <el-col
                :span="24"
              >评论内容：{{ scope.row.commentContent }}</el-col>
              <el-col :span="24">评论人：{{ scope.row.userRealName }}</el-col>
              <el-col :span="12">评论状态：{{ scope.row.statusName }}</el-col>
            </template>
          </el-table-column>
          <el-table-column prop="userImgs" label="评论图片">
            <template slot-scope="scope">
              <el-image
                v-for="(item,index) in scope.row.userImgs
                  ? scope.row.userImgs.split(',')
                  : []"
                :key="index"
                class="comment-img"
                :z-index="99999"
                :src="view + item"
                :preview-src-list="[view + item]"
              >
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="crtTime" label="评论时间" />
          <el-table-column label="操作" fixed="right" width="80">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="handleUpdate(scope.row)"
              >修改状态</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total4parent > 0"
        :total="total4parent"
        :page.sync="listQuery4parent.page"
        :limit.sync="listQuery4parent.limit"
        @pagination="getList4parent"
      />
      <div slot="footer" class="dialog-footer">
        <el-button
          style="padding: 8px 30px"
          @click="dialog4commentParent = false"
        >关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { queryPage4comment, getDictByCode, update4comment, queryPage4commentParent } from '../api'
import permission from '@/directive/permission/index.js'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'Hdbmcomment',
  components: { Pagination },
  directives: { permission, elDragDialog },
  data() {
    return {
      newsId: '',
      listQuery: {
        page: 1,
        limit: 10,
        commentContent: ''
      },
      searchshow: true,
      search: {
        mainTitle: '',
        source: '',
        publisher: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      view: '/API-DOCUMENT/document/file/download?id=',
      commentItem: {},
      dialog4comment: false,
      commentStatus: '',
      dialog4commentParent: false,
      dict: {
        PLZT: []
      },
      list4parent: [],
      listQuery4parent: {
        page: 1,
        limit: 10,
        commentContent: ''
      },
      total4parent: 0,
      listLoading4parent: false,
      parentId: ''
    }
  },
  created() {
    this.newsId = this.$route.query.i
    this.getList()
    for (const i in this.dict) {
      this.getDict(i)
    }
  },
  methods: {
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code).then(response => {
          this.dict[code] = response.data.data[0].list
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        newsId: this.newsId
      }
      if (this.searchshow) {
        query['commentContent'] = this.listQuery.commentContent
      } else {
        query['mainTitle'] = this.search.mainTitle
        query['source'] = this.search.source
        query['publisher'] = this.search.publisher
      }
      return new Promise((resolve, reject) => {
        queryPage4comment(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    },
    getList4parent(obj) {
      this.listLoading4parent = true
      var query = {
        page: obj ? obj.page : this.listQuery4parent.page,
        pageSize: obj ? obj.limit : this.listQuery4parent.limit,
        newsId: this.newsId,
        id: this.parentId
      }
      return new Promise((resolve, reject) => {
        queryPage4commentParent(query).then(response => {
          this.list4parent = response.data.data.rows
          this.total4parent = response.data.data.total
          this.listLoading4parent = false
        }).catch(error => {
          reject(error)
          this.listLoading4parent = false
        })
      })
    },
    filtercolumn(scope, item) {
      return scope.row[item]
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.userName = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleUpdate(row) {
      this.commentStatus = row.status
      this.commentItem = Object.assign({}, row)
      this.dialog4comment = true
    },
    handleView(row) {
      this.parentId = row.id
      this.commentItem = Object.assign({}, row)
      this.dialog4commentParent = true
      this.getList4parent()
    },
    handleComit() {
      this.$confirm('确认修改此评论状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          update4comment({ id: this.commentItem.id, status: this.commentStatus }).then(res => {
            loading.close()
            if (res.data.code === '200') {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.dialog4comment = false
              this.getList()
              if (this.parentId) {
                this.getList4parent()
              }
            }
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch()
    }

  }
}
</script>

<style lang="scss">
.hdbm-comment {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: "";
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .main-buttons {
    display: flex;
    line-height: 36px;
    margin: 10px 0;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
  .main-table {
    .comment-img {
      width: auto;
      height: auto;
      margin-right: 10px;
      > img {
        width: 100px;
        height: 100px;
      }
      .el-icon-circle-close {
        color: #fff;
      }
    }
  }
}
</style>
